﻿using AutoMapper;
using NFine.Data.Dto.SystemManage;
using NFine.Data.Entity.SystemManage;
using NFine.Repository.SystemManage;
using NFine.Util;
using SharpRepository.Repository.Queries;
using SharpRepository.Repository.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;

namespace NFine.Service.SystemManage
{
    public class TenantService
    {
        TenantRepository tenantRepository = new TenantRepository(SharpRepoConfig.sharpRepoConfig, "efCore");
        RoleRepository roleRepository = new RoleRepository(SharpRepoConfig.sharpRepoConfig, "efCore");
        UserRepository userRepository = new UserRepository(SharpRepoConfig.sharpRepoConfig, "efCore");

        //public List<RoleGridDto> GetList()
        //{
        //    var spec = new Specification<Role>();
        //    spec = new Specification<Role>(p => p.EnabledMark.Equals(true));
        //    var list = roleRepository.FindAll(spec).ToList();
        //    return Mapper.Map<List<RoleGridDto>>(list);
        //}

        //public List<RoleGridDto> GetList(string keyword)
        //{
        //    var spec = new Specification<Role>();
        //    if (!string.IsNullOrEmpty(keyword))
        //    {
        //        spec = new Specification<Role>(p => p.FullName.Contains(keyword) || p.EnCode.Contains(keyword));
        //    }
        //    var list = roleRepository.FindAll(spec).ToList();
        //    return Mapper.Map<List<RoleGridDto>>(list);
        //}

        public List<TenantGridDto> GetList(Pagination pagination, string keyword)
        {
            var specification = new Specification<Tenant>();
            if (!string.IsNullOrEmpty(keyword))
            {
                specification = new Specification<Tenant>(r => (r.FullName.Contains(keyword) || r.TenantName.Contains(keyword)));
            }
            var pagingOptions = new PagingOptions<Tenant, DateTime?>(pagination.page, pagination.rows, x => x.CreatedTime, isDescending: true);
            var list = tenantRepository.FindAll(specification, pagingOptions).ToList();
            pagination.records = pagingOptions.TotalItems;
            return Mapper.Map<List<TenantGridDto>>(list);
        }
        public Tenant GetEntity(long keyValue)
        {
            var id = Convert.ToInt64(keyValue);
            Tenant tenant = tenantRepository.Get(id);
            return tenant;
        }
        public Tenant FindEntity(string tenantName)
        {
            Tenant tenant = tenantRepository.Find(t=>t.TenantName==tenantName);
            return tenant;
        }
        public TenantOutputDto GetForm(string keyValue)
        {
            var id = Convert.ToInt64(keyValue);
            TenantOutputDto tenantOutputDto = new TenantOutputDto();
            Tenant tenant = tenantRepository.Get(id);
            AutoMapper.Mapper.Map<Tenant, TenantOutputDto>(tenant, tenantOutputDto);
            return tenantOutputDto;
        }

        public void SubmitForm(TenantInputDto tenantInputDto, string keyValue)
        {
            Tenant tenant = new Tenant();
            long id = Convert.ToInt64(keyValue);
            long userId = OperatorProvider.Provider.GetOperator().Id;
            if (!string.IsNullOrEmpty(keyValue))
            {
                tenant = tenantRepository.Get(id);
                AutoMapper.Mapper.Map<TenantInputDto, Tenant>(tenantInputDto, tenant);
                tenant.ModifierUserId = userId;
                tenant.ModifiedTime = DateTime.Now;
                tenantRepository.Update(tenant);
            }
            else
            {
                tenantRepository.CreateTenant(tenantInputDto);
            }
        }

        public void DeleteForm(string keyValue)
        {
            long userId = OperatorProvider.Provider.GetOperator().Id;
            var id = Convert.ToInt64(keyValue);
            Tenant tenant = tenantRepository.Get(id);
            tenant.DeletedMark = true;
            tenant.DeleterUserId=OperatorProvider.Provider.GetOperator().Id;
            tenant.DeletedTime = DateTime.Now;
            tenantRepository.Update(tenant);
        }

        public void EnabledForm(string keyValue)
        {
            var id = Convert.ToInt64(keyValue);
            Tenant tenant = tenantRepository.Get(id);
            tenant.EnabledMark = true;
            tenant.ModifierUserId = OperatorProvider.Provider.GetOperator().Id;
            tenant.ModifiedTime = DateTime.Now;
            tenantRepository.Update(tenant);
        }

        public void DisabledForm(string keyValue)
        {
            var id = Convert.ToInt64(keyValue);
            Tenant tenant = tenantRepository.Get(id);
            tenant.EnabledMark = false;
            tenant.ModifierUserId = OperatorProvider.Provider.GetOperator().Id;
            tenant.ModifiedTime = DateTime.Now;
            tenantRepository.Update(tenant);
        }

        public Tenant VaildTenantNameExists(string tenantname)
        {
            Tenant tenant = tenantRepository.Find(t => t.TenantName == tenantname);
            return tenant;
        }
    }
}
